package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private  ClazzServiceImpl clazzService;

    public StudentServiceImpl(ClazzServiceImpl clazzService) {
        this.clazzService = clazzService;
    }

    /**
     * 分页查询
     *
     * @param param 查询条件
     * @return 分页结果
     */
    @Override
    public PageResult<Student> getPageResult(StudentQueryParam param) {
        Page<Student> page = Page.of(param.getPage(), param.getPageSize());

        // 根据最后修改时间倒序排序
        page.addOrder(OrderItem.desc("update_time"));

        // 使用lambdaQuery方法构建查询条件
        page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Student::getName, param.getName())
                .eq(param.getDegree() != null, Student::getDegree, param.getDegree())
                .eq(param.getClazzId() != null, Student::getClazzId, param.getClazzId())
                .page(page);

        // 获取学生集合
        List<Student> studnets = page.getRecords();

        //获取所有学生班级的id
        List<Long> clazzIds = studnets.stream().map(student -> student.getClazzId()).collect(Collectors.toList());

        if(!clazzIds.isEmpty()){
            //批量查询这些班级的信息
            List<Clazz> clazzs = clazzService.listByIds(clazzIds);

            //将班级名称封装到员工对象中
            studnets.forEach(student -> {
                clazzs.forEach(clazz -> {
                    if(student.getClazzId() != null && student.getClazzId().equals(clazz.getId())){
                        student.setClazzName(clazz.getName());
                    }
                });
            });
        }

        return new PageResult<>(page.getTotal(), studnets);
    }

    /**
     * 新增学生
     *
     * @param student 学生信息
     */
    @Override
    public void saveStu(Student student) {
        // 调用service层方法保存员工信息

        // 补全基础属性
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());


        save(student);


    }

    /**
     * 根据id查询学生信息
     *
     * @param id 学生id
     * @return 查询到的学生信息
     */
    @Override
    public Student getStuById(Long id) {
        Student studnet = getById(id);

        return studnet;
    }

    /**
     * 修改员工
     *
     * @param student 学生信息
     */
    @Override
    public void updateStu(Student student) {
        // 调用service层方法修改员工信息
        //根据id更新学生的基本信息
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    /**
     * 批量删除学生
     *
     * @param ids 要删除的学生id
     */
    @Override
    public void removeStuByIds(List<Long> ids) {
        // 调用service层方法批量删除员工信息
        removeByIds(ids);
    }

    //违纪扣分
    @Override
    public void violation(Integer id, Integer score) {
        Student student = getById(id);
        //用 LambdaQueryWrapper 来更新 Student 的违纪扣分，违纪次数是violationCount，违纪分数是violationScore(正分制)
        //违纪次数加一
        student.setViolationCount((short) (student.getViolationCount() + 1));
        //违纪分数加score
        student.setViolationScore((student.getViolationScore() + score));
        //更新时间
        student.setUpdateTime(LocalDateTime.now());
        //更新数据
        this.updateById(student);

    }


}
